package src.数据结构.链表.单向链表;

import src.数据结构.链表.Node;

public class JosephRing extends LinkList{
    public void Joseph() {
        Node n = this.head.next;
        Node before = null;
        int count = 0;
        while (n!= n.next){
            // 创建计算器，模拟报数,如果count为3则删除当前节点，并重置count
            count++;
            if(count==3){
                before.next = n.next;
                System.out.print(n.item+",");
                count=0;
                n = n.next;
            }else {
                before = n;
                n = n.next;
            }
        }
        System.out.println(n.item);
    }

    public static void main(String[] args) {
        JosephRing linkList = new JosephRing();
        // 创建节点
        for (int i = 1; i <= 41; i++) {
            linkList.add(i);
        }
        // 创建循环链表
        linkList.ring();
        linkList.Joseph();
    }
}
